package com.naodong.shenluntiku.intergation.alipush;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.HuaWeiRegister;
import com.alibaba.sdk.android.push.register.MiPushRegister;
import com.naodong.shenluntiku.BuildConfig;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.data.manager.UserInfoManager;

/**
 * Created by hujie on 17/6/7.
 */

public class PushManager {

    private static PushManager instance;

    private CloudPushService pushService;
    private boolean isInitSuccess;
    private boolean isBindSuccess;

    public static PushManager getInstance() {
        if (instance == null) {
            instance = new PushManager();
        }
        return instance;
    }

    private PushManager() {

    }


    /**
     * 初始化推送
     * @param applicationContext
     */
    public void initPush(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        pushService = PushServiceFactory.getCloudPushService();

        pushService.setNotificationSmallIcon(R.drawable.ic_notification);
        String soundFilePath = "android.resource://" + applicationContext.getPackageName() + "/" + R.raw.notification_sound;
        pushService.setNotificationSoundFilePath(soundFilePath);

        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                registerThirdPush(applicationContext);
                isInitSuccess = true;
                bindAccount();
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.e("shenluntikuapp", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }

    private void registerThirdPush(Context applicationContext) {
        MiPushRegister.register(applicationContext, BuildConfig.XIAOMI_APPID, BuildConfig.XIAOMI_APPKEY);
        HuaWeiRegister.register(applicationContext);

    }


    /**
     * 绑定设备
     */
    public void bindAccount() {
        if (!isBindSuccess && isInitSuccess && UserInfoManager.getInstance().getUserInfo() != null && UserInfoManager.getInstance().getUserInfo().getAcUuid() != null) {
            pushService.bindAccount(UserInfoManager.getInstance().getUserInfo().getAcUuid(), new CommonCallback() {
                @Override
                public void onSuccess(String s) {
                    isBindSuccess = true;
                    Log.d("shenluntikuapp", "bind account success");
                }

                @Override
                public void onFailed(String s, String s1) {
                    Log.e("shenluntikuapp", "bind account failed :" + s + "---" + s1);
                }
            });
        }
    }

    /**
     * 解绑操作
     */
    public void unBindAccount() {
        pushService.unbindAccount(new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.d("shenluntikuapp", "unbind account success");
            }

            @Override
            public void onFailed(String s, String s1) {
                Log.e("shenluntikuapp", "unbind account failed :" + s + "---" + s1);
            }
        });
    }


    /**
     * 重置推送
     */
    public void resetPush() {
        isInitSuccess = false;
        isBindSuccess = false;
        unBindAccount();
    }


}
